﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using log4net;

namespace Estimate.Common.DataFileFinder
{
    /// <summary>
    /// 国家局指导预报
    /// </summary>
    public class ZytCityForeFinder : IDataFileFinder
    {
        public static readonly ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string filterFiles(string dirPath, string[] filepattern)
        {
            if (Directory.Exists(dirPath))
            {
                string[] fileArray = getFileList(dirPath, filepattern);
                if( fileArray.Length > 0)
                    return fileArray[fileArray.Length - 1]; 
            }
            else
            {
                Logger.Error(string.Format("无法找到文件夹 {0}!", dirPath));
            }
            return "";
        }

        public string[] GetFileFromDirectory(DateTime dt, string dirPath)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");
            List<string> result = new List<string>();

            //早晨06点预报
            string f = filterFiles( dirPath, new string[] 
                {
                    "Z_SEVP_C_BABJ_" + date0 + "18????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                     "Z_SEVP_C_BABJ_" + date0 + "19????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                     "Z_SEVP_C_BABJ_" + date0 + "20????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT"
                });
            if( f!= "")
                result.Add(f);

            //下午16点预报
            f = filterFiles(dirPath, new string[] 
                {
                    "Z_SEVP_C_BABJ_" + date + "06????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT",
                    "Z_SEVP_C_BABJ_" + date + "07????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT"            
                });
            if (f != "")
                result.Add(f);           

            return result.ToArray();
        }

        public Dictionary<string, string[]> GetFileListFromDirectory(DateTime dt, string dirPath)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");
            Dictionary<string, string[]> result = new Dictionary<string, string[]>();

            //早晨06点预报
            string[] f = getFileList(dirPath, new string[] 
                {
                    //"Z_SEVP_I_BENJ_P_RFFC-AVEECWMF-201406100000-19203.TXT"
                    //"Z_SEVP_I_BENJ_P_RFFC-AVEECWMF-201406101200-19203.TXT"
                    "Z_SEVP_C_BENJ_20140609201906_P_RFFC-SCMOC-201406100000-16812.txt"
                    //"Z_SEVP_C_BABJ_" + date0 + "18????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                    // "Z_SEVP_C_BABJ_" + date0 + "19????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                    // "Z_SEVP_C_BABJ_" + date0 + "20????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT"
                });
            result.Add("16812_06", f);

            //下午16点预报
            f = getFileList(dirPath, new string[] 
                {
                    "Z_SEVP_C_BABJ_" + date + "06????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT",
                    "Z_SEVP_C_BABJ_" + date + "07????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT"            
                });
            result.Add("16812_16", f);

            return result;
        }

        public string[] getFileList(string dirPath, string[] filepattern)
        {
            List<string> files = new List<string>();
            foreach (string pattern in filepattern)
            {
                if (Directory.Exists(dirPath))
                {
                    files.AddRange(Directory.GetFiles(dirPath, pattern));
                }
                else
                {
                    Logger.Error(string.Format("无法找到文件夹 {0}!", dirPath));
                }

            }
            if (files.Count == 0)
                return new string[] { };

            string[] fileArray = files.ToArray();
            //对文件组排序
            for (int i = 0; i < fileArray.Length; i++)
            {
                for (int j = i + 1; j < fileArray.Length; j++)
                {
                    if (files[i].CompareTo(files[j]) > 0)
                    {
                        string temp = files[i];
                        files[i] = files[j];
                        files[j] = temp;
                    }
                }
            }

            return fileArray;
        }

        public string[] GetFileNames(DateTime dt)
        {
            throw new NotImplementedException();
        }
    }
}
